<template>
    <el-upload class="avatar-uploader" action="http://goapi.sanjitian.cn/api/files" :on-success="handleAvatarSuccess"
        :headers="headers" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
            <Plus />
        </el-icon>
    </el-upload>
    <div class="my-img-list">
        <div v-for="item in myImgList" :key="item.id" class="img-container">
            <img :src="item.filePathCos" :alt="item.fileType" />
            <el-icon class="close-btn" @click="removeImage(item)">
                <close />
            </el-icon>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'

interface IFileInfo {
    fileExtension: string
    filePathCos: string
    fileSize: string
    fileType: string
    fileUse: string
    id: number
    mineType: string
    uploadTime: string
}

const imageUrl = ref('')
const myImgList = ref<IFileInfo[]>([])

const headers = {
    Authorization: `${localStorage.getItem('token')}`
}

const removeImage = (item: IFileInfo) => {
    myImgList.value = myImgList.value.filter(i => i.id !== item.id)
}

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
    imageUrl.value = URL.createObjectURL(uploadFile.raw!);
    myImgList.value.push(response.data)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}
</script>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}

.my-img-list {
    width: 100%;
    height: 200px;
    display: flex;
    flex-wrap: wrap;

    img {
        height: 100%;
    }
}
</style>
<style lang="scss" scoped>
.my-img-list {
    width: 100%;
    height: 200px;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;

    .img-container {
        position: relative;
        display: inline-block;
        overflow: hidden;
    }

    img {
        height: 150px;
        width: 150px;
    }

    .close-btn {
        font-size: 20px;
        position: absolute;
        top: 5px;
        right: 5px;
        cursor: pointer;
        color: #f56c6c;
        opacity: 0;
        transition: opacity 0.3s;
    }

    .img-container:hover .close-btn {
        opacity: 1;
    }
}
</style>